/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package compilador.analisadorSintatico.classe;

import compilador.analisadorLexico.Token;
import compilador.analisadorLexico.TokenClass;
import compilador.analisadorSintatico.Producao;
import compilador.analisadorSintatico.TokenReader;
import compilador.analisadorSintatico.simboloGramatical.NaoTerminal;
import compilador.analisadorSintatico.simboloGramatical.SimboloGramatical;
import compilador.analisadorSintatico.simboloGramatical.Terminal;

/**
 *
 * @author Matheus Cardoso
 */
public class DEC_PARAMETROS extends Producao{

    @Override
    public void setSimbolo() {
        this.simbolo = new NaoTerminal(this.getClass().getSimpleName());
    }

    
    
    @Override
    public SimboloGramatical derivar() throws Exception {
        TokenReader tokenReader = TokenReader.getInstance();
        
        Token token = null;
        
        token = tokenReader.seeCurrent();
        
        NaoTerminal naoTerminal = (NaoTerminal) this.simbolo;
        naoTerminal.add(new TIPO().derivar());
        
        token = tokenReader.seeCurrent();
        if(token.getTokenClass().equals(TokenClass.OPERADOR) && token.getAtributo().equals("[")){
            
            tokenReader.consome();
            naoTerminal.add(new Terminal(token));
            
            token = tokenReader.seeCurrent();
            if(token.getTokenClass().equals(TokenClass.OPERADOR) && token.getAtributo().equals("]")){
                tokenReader.consome();
                naoTerminal.add(new Terminal(token));
                
            } else {
                erro(this.getClass().getSimpleName() + " mal formada - ']' não encontrado");
                return simbolo;
            }
        }
        naoTerminal.add(new NOME().derivar());
        
        token = tokenReader.seeCurrent();
        while((token.getTokenClass().equals(TokenClass.OPERADOR)) && (token.getAtributo().equals(",")) && tokenReader.hasToken()){
            if(token.getTokenClass().equals(TokenClass.OPERADOR) && token.getAtributo().equals(",")){

                tokenReader.consome();
                naoTerminal.add(new Terminal(token));

                token = tokenReader.seeCurrent();
                naoTerminal.add(new TIPO().derivar());

                token = tokenReader.seeCurrent();
                if(token.getTokenClass().equals(TokenClass.OPERADOR) && token.getAtributo().equals("[")){

                    tokenReader.consome();
                    naoTerminal.add(new Terminal(token));

                    token = tokenReader.seeCurrent();
                    if(token.getTokenClass().equals(TokenClass.OPERADOR) && token.getAtributo().equals("]")){
                        tokenReader.consome();
                        naoTerminal.add(new Terminal(token));

                    }else{
                        erro(this.getClass().getSimpleName() + " mal formada - ']' não encontrado");
                        return this.simbolo;
                    }
                }
                naoTerminal.add(new NOME().derivar());
            }
            
            /*token = tokenReader.seeCurrent();
            naoTerminal.add(new NOME().derivar());*/
        }
        
        return this.simbolo;
    }

}
